package org.nobject.common.file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

/**
 * 配置文件工具
 *
 * @author bianrongjun
 * @version 1.0
 */
public class PropertiesUtils {

	/**
	 * 读取配置文件
	 * 
	 * @param filePath 文件路径
	 */
	public static Properties read(String filePath) {
		return read(new File(filePath));
	}
	
	/**
	 * 读取配置文件
	 * 
	 * @param file 文件
	 */
	public static Properties read(File file) {
		Properties props = new Properties();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(file));
			props.load(in);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return props;
	}
	
	/** 
	 * 读取配置文件
	 * @param is 输入流
	 */
	public static Properties read(InputStream is) {
		Properties props = new Properties();
		try {
			InputStream in = new BufferedInputStream(is);
			props.load(in);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return props;
	}
	
	/** 
	 * 写入配置文件
	 * @param filePath
	 * @param parameterName
	 * @param parameterValue
	 */
	public static void write(String filePath, String parameterName, String parameterValue) {
		Properties prop = new Properties();
		try {
			OutputStream fos = new FileOutputStream(filePath);
			InputStream fis = new FileInputStream(filePath);
			prop.load(fis);
			prop.setProperty(parameterName, parameterValue);
			prop.store(fos, "");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}
